package com.huang.config.exception;

import com.huang.enums.ResultStatus;
import com.huang.vo.ResultObject;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;

/**
 * 处理没有捕获到的异常
 */
@RestController
public class DefaultErrorController implements ErrorController {

    @RequestMapping("/error")
    public ResultObject<Boolean> handleError(HttpServletRequest request){
        Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
        if (status != null) {
            int statusCode = Integer.parseInt(status.toString());
            if (statusCode == HttpStatus.NOT_FOUND.value()) {
                return ResultObject.fail(ResultStatus.SYS_NOT_FOUND);
            }
            return ResultObject.fail(ResultStatus.SYS_UNKNOWN_ERROR);
        }
        return ResultObject.fail(ResultStatus.SYS_UNKNOWN_ERROR);
    }

//    @Override
//    public String getErrorPath() {
//        return null;
//    }
}
